package com.yilin.medical.activity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;

import com.yilin.medical.R;

public class FaBuBingLiActivity extends BaseActivity implements
		OnItemSelectedListener {

	private TextView mTextViewYuYueDate, mTextViewYuYueTime;
	private Context context;
	private List<String> list = new ArrayList<String>();
	private Spinner mySpinner;
	private ArrayAdapter<String> adapter;
	private LinearLayout mLinearLayoutYuYueDate, mLinearLayoutYuYueTime;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentViewWithTitle(R.layout.activity_fabubingli);
		context = this;
		initView();
		initData();
		initAdapter();
		registerLintener();
	}

	private void initView() {
		mTitle.getTitle().setText("发布病例");
		mySpinner = (Spinner) findViewById(R.id.Spinner_binglileixing);
		mLinearLayoutYuYueDate = (LinearLayout) findViewById(R.id.ll_yuyuehuizhendate);
		mLinearLayoutYuYueTime = (LinearLayout) findViewById(R.id.ll_yuyuehuizhentime);
		mTextViewYuYueDate = (TextView) findViewById(R.id.tv_yuyuehuizhendate);
		mTextViewYuYueTime = (TextView) findViewById(R.id.tv_yuyuehuizhentime);
	}

	private void initData() {
		list.add("第1种病例");
		list.add("第2种病例");
		list.add("第3种病例");
		list.add("第4种病例");
		list.add("第5种病例");
	}

	private void initAdapter() {
		// 第二步：为下拉列表定义一个适配器，这里就用到里前面定义的list。
		adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, list);
		// 第三步：为适配器设置下拉列表下拉时的菜单样式。
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// 第四步：将适配器添加到下拉列表上
		mySpinner.setAdapter(adapter);
	}

	private void registerLintener() {
		mySpinner.setOnItemSelectedListener(this);
		mLinearLayoutYuYueDate.setOnClickListener(this);
		mLinearLayoutYuYueTime.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		Intent intent;
		super.onClick(v);
		switch (v.getId()) {
		case R.id.ll_yuyuehuizhendate:
			updateDate();
			break;
		case R.id.ll_yuyuehuizhentime:
			getTimePiker();
			break;
		}
	}

	private void updateDate() {
		final Calendar cal = Calendar.getInstance();
		new DatePickerDialog(mContext,
				new DatePickerDialog.OnDateSetListener() {
					@Override
					public void onDateSet(DatePicker view, int year,
							int monthOfYear, int dayOfMonth) {
						cal.set(Calendar.YEAR, year);
						cal.set(Calendar.MONTH, monthOfYear);
						cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
						mTextViewYuYueDate.setText(new SimpleDateFormat(
								"yyyy-MM-dd").format(cal.getTime()));
					}
				}, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
				cal.get(Calendar.DAY_OF_MONTH)).show();
	}

	private void getTimePiker() {
		TimePickerDialog datePicker = new TimePickerDialog(this,
				new OnTimeSetListener() {

					@Override
					public void onTimeSet(TimePicker view, int hourOfDay,
							int minute) {
						Calendar c = Calendar.getInstance();
						c.set(Calendar.HOUR_OF_DAY, hourOfDay);
						c.set(Calendar.MINUTE, minute);

						mTextViewYuYueTime
								.setText(new SimpleDateFormat("HH:mm").format(c
										.getTime()));
					}
				}, 0, 0, true);
		datePicker.show();
	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View selectview,
			int positon, long arg3) {
		selectview.setVisibility(View.VISIBLE);
	}

	@Override
	public void onNothingSelected(AdapterView<?> parent) {
		parent.setVisibility(View.VISIBLE);
	}

}
